package com.htgd.gardenexpo.utils;

import com.htgd.gardenexpo.entity.CommonResult;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * @author ：hejing
 * @date ：Created in 2021/7/28 18:06
 * @description：全局异常捕获
 * @modified By：`
 * @version: 1.0
 */
@RestControllerAdvice
public class MyExceptionHandler {

    @Value("${spring.servlet.multipart.max-file-size}")
    public String limit;

    /* spring默认上传大小1MB 超出大小捕获异常MaxUploadSizeExceededException */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public CommonResult<JSONObject> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        CommonResult<JSONObject> result = new CommonResult<>();
        result.setCode(300);
        result.setMessage("文件大小超出"+limit+"限制，请压缩或降低文件质量!");
        return result;
    }
}
